package com.kaibes.module.user;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

import org.springframework.stereotype.Service;

import com.kaibes.core.base.option.NullOptions;
import com.kaibes.core.base.util.MathUtils;
import com.kaibes.core.base.util.UuidUtils;
import com.kaibes.module.user.pojo.User;
import com.kaibes.module.user.pojo.UserInsert;
import com.kaibes.orm.core.core.Page;
import com.kaibes.orm.core.util.PageUtils;
import com.kaibes.orm.curd.QueryBuilder;
import com.kaibes.orm.curd.SelectBuilder;
import com.kaibes.orm.curd.UpdateBuilder;
import com.kaibes.web.api.ResponseServiceImpl;

@Service
public class UserService extends ResponseServiceImpl<User> {
    
    public int updateLoginTimeById(Integer id, LocalDateTime time) {
        User user = new User();
        user.setLoginTime(time);
        return super.updateById(user, id);
    }

    public int updateLogoutTimeById(Integer id, LocalDateTime time) {
        User user = new User();
        user.setLogoutTime(time);
        return super.updateById(user, id);
    }

    public boolean updateImage(Integer id, String image) {
        return mapper.update(UpdateBuilder.newBuilder().addQuery(QueryBuilder.newBuilder().eq("id", id).build())
                .setValue("image", image).build()) > 0;
    }

    public long countByNicknameAndNotId(Integer id, String nickname) {
        return count(QueryBuilder.newBuilder().ne("id", id).eq("nickname", nickname).build());
    }

    public Integer getRoleIdById(Integer id) {
        return NullOptions.returnInteger(getById(id, "role_id"), user -> user.getRoleId());
    }

    public Page<User> pageNickname(Integer current, Integer size) {
        return getMapper().page(SelectBuilder.newBuilder().addColumn("id").addColumn("nickname").build(), current,
                size);
    }

    public Page<User> pageNicknameByNicknameLike(Integer current, Integer size, String search) {
        return getMapper().page(SelectBuilder.newBuilder().addColumn("id").addColumn("nickname")
                .addQuery(QueryBuilder.newBuilder().like("nickname", search).build()).build(), current, size);
    }

    public Page<User> pageByNicknameLike(Integer current, Integer size, String search) {
        return page(current, size, QueryBuilder.newBuilder().like("nickname", search).build());
    }

    public Page<User> pageById(Integer current, Integer size, Integer id) {
        return PageUtils.createOnePage(getById(id));
    }

    public Page<User> pageByIdOrNicknameLike(Integer current, Integer size, String search) {
        if (MathUtils.isInteger(search)) {
            return pageById(current, size, Integer.parseInt(search));
        } else {
            return pageByNicknameLike(current, size, search);
        }
    }

    public Page<User> pageByRoleId(Integer current, Integer size, Integer roleId) {
        return page(current, size, QueryBuilder.newBuilder().eq("role_id", roleId).build());
    }

    public Page<User> pageByGender(Integer current, Integer size, GenderEnum gender) {
        return page(current, size, QueryBuilder.newBuilder().eq("gender", gender).build());
    }

    public String getNicknameById(Integer id) {
        return NullOptions.returnString(getById(id, "nickname"), user -> user.getNickname());
    }

    public String getImageById(Integer id) {
        return NullOptions.returnString(getById(id, "image"), user -> user.getImage());
    }
    
    public User getNicknameAndImageById(Integer id) {
        return getById(id, "nickname", "image");
    }

    public boolean updateRoleIdById(Integer roleId, Integer id) {
        return mapper.update(UpdateBuilder.newBuilder().addQuery(QueryBuilder.newBuilder().eq("id", id).build())
                .setValue("role_id", roleId).build()) > 0;
    }

    public boolean updateRoleIdById(Integer roleId, Integer... ids) {
        return mapper.update(UpdateBuilder.newBuilder().addQuery(QueryBuilder.newBuilder().inArray("id", ids).build())
                .setValue("role_id", roleId).build()) > 0;
    }

    public boolean updateRoleIdByRoleId(List<?> oldRoleIds, Integer newRoleId) {
        return mapper.update(
                UpdateBuilder.newBuilder().addQuery(
                        QueryBuilder.newBuilder().inList("role_id", oldRoleIds).build())
                        .setValue("role_id", newRoleId).build()) > 0;
    }

    public boolean updateRoleIdNull(List<Integer> roleIdList) {
        return mapper.update(
                UpdateBuilder.newBuilder().addQuery(QueryBuilder.newBuilder().inList("role_id", roleIdList).build())
                        .setNull("role_id").build()) > 0;
    }

    public Integer createDefaultUser() {
        UserInsert user = new UserInsert();
        user.setRoleId(1);
        user.setName("匿名");
        user.setNickname("新用户_" + UuidUtils.getUUIDNoLine());
        user.setGender(GenderEnum.FEMAL);
        user.setBirth(LocalDate.now());
        user.setImage("/images/head.jpg");
        user.setAddress("");
        user.setIntroduction("");
        List<Number> result = save(user);
        return (result != null && result.size() > 0) ? result.get(0).intValue() : 0;
    }

}
